package server;

import java.io.IOException;
import java.io.InputStream;

public class Request {
    private String method ;//GET OR POST
    private String url;// 获取资源位置
    private InputStream inputStream ;

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int available = 0;
        while (available==0){
            available = inputStream.available();
        }
        byte[] bytes = new byte[available];//根据长度生成数据。

        int read = inputStream.read(bytes);//读取数据，到bytes里面
        String inputStr = new String(bytes);//
        /**
         * 请求首行
         * 首行格式   GET / HTTP/1.1
         * *ps: 使用‘空格’作为区分元素，所以首行共有3个元素
         * GET :方法
         *  / : url
         * HTTP/1.1 :协议
         */
        String firstLine = inputStr.split("\\n")[0];
        String[] s = firstLine.split(" ");
        String method  = s[0];
        String url = s[1];
        System.out.println("method: "+ method +" url: "+url);
        this.method = method;
        this.url = url;
    }

    public Request(){

    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
